#请求头可以不传，会自动生成
#登录接口调用
import json

import requests
from common.DBUtil import DBUtil

url_login = "http://ihrm-test.itheima.net/api/sys/login"
headers = {"Content-Type": "application/json"}
body = {"mobile": "13800000002", "password": "123456"}

resp = requests.post(url_login, headers=headers, json=body) #发送请求
print(resp.json())
token = "Bearer " + resp.json().get("data")
print(token)


#新增员工接口(需要令牌)
sql = "DELETE FROM bs_user WHERE mobile = '13121213131';"
DBUtil.uid_db(sql)

url_add_emp = "http://ihrm-test.itheima.net/api/sys/user"
headers = {"Authorization":token}
body_add = {"username":"美羊羊","mobile": "13121213131", "workNumber":"1232"}
resp_add = requests.post(url_add_emp, headers=headers, json=body_add)
print(resp_add.json())
id = resp_add.json().get('data').get('id')

#员工管理-查询员工接口
url_get = f"http://ihrm-test.itheima.net/api/sys/user/{id}"
resp_get = requests.get(url_get,headers=headers)
print("查询结果：",resp_get.json())


##员工管理-修改员工接口
url_xg = f"http://ihrm-test.itheima.net/api/sys/user/{id}"
body_xg = {"username":"喜羊羊","password": "112211", "departmentId":"131214"}
resp_xg = requests.put(url_xg,headers=headers,json=body_xg)
print('修改结果：',resp_xg.json())


##员工管理-删除员工接口
url_delete = f"http://ihrm-test.itheima.net/api/sys/user/{id}"
resp_delete = requests.put(url_xg,headers=headers)
print('删除结果：',resp_delete.json())